from sqlalchemy import Column, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from init.mysql import engine

Base = declarative_base()


class CompanyInfo(Base):
    __tablename__ = 'company_info'
    code = Column(String(6), primary_key=True)
    name = Column(String(50))
    ename = Column(String(50))
    market = Column(String(20))
    idea = Column(String(100))
    ldate = Column(String(10))
    sprice = Column(String(10))
    principal = Column(String(20))
    rdate = Column(String(10))
    rprice = Column(String(10))
    instype = Column(String(20))
    organ = Column(String(20))
    secre = Column(String(20))
    phone = Column(String(20))
    sphone = Column(String(20))
    fax = Column(String(20))
    sfax = Column(String(20))
    email = Column(String(50))
    semail = Column(String(50))
    site = Column(String(50))
    post = Column(String(10))
    infosite = Column(String(50))
    oname = Column(String(20))
    addr = Column(String(100))
    oaddr = Column(String(100))
    desc = Column(String(200))
    bscope = Column(String(100))
    printype = Column(String(20))
    referrer = Column(String(20))
    putype = Column(String(20))
    pe = Column(String(20))
    firgu = Column(String(20))
    lastgu = Column(String(20))
    realgu = Column(String(20))
    planm = Column(String(20))
    realm = Column(String(20))
    pubfee = Column(String(20))
    collect = Column(String(20))
    signfee = Column(String(20))
    pdate = Column(String(20))

    def __init__(self,code, name, ename, market, idea, ldate, sprice, principal, rdate, rprice, instype, organ, secre, phone,
                 sphone, fax, sfax, email, semail, site, post, infosite, oname, addr, oaddr, desc, bscope, printype,
                 referrer, putype, pe, firgu, lastgu, realgu, planm, realm, pubfee, collect, signfee, pdate):
        self.code = code
        self.name = name
        self.ename = ename
        self.market = market
        self.idea = idea
        self.ldate = ldate
        self.sprice = sprice
        self.principal = principal
        self.rdate = rdate
        self.rprice = rprice
        self.instype = instype
        self.organ = organ
        self.secre = secre
        self.phone = phone
        self.sphone = sphone
        self.fax = fax
        self.sfax = sfax
        self.email = email
        self.semail = semail
        self.site = site
        self.post = post
        self.infosite = infosite
        self.oname = oname
        self.addr = addr
        self.oaddr = oaddr
        self.desc = desc
        self.bscope = bscope
        self.printype = printype
        self.referrer = referrer
        self.putype = putype
        self.pe = pe
        self.firgu = firgu
        self.lastgu = lastgu
        self.realgu = realgu
        self.planm = planm
        self.realm = realm
        self.pubfee = pubfee
        self.collect = collect
        self.signfee = signfee
        self.pdate = pdate


# 创建数据库表
Base.metadata.create_all(engine)


def insert_or_update(code,data):
    session = sessionmaker(bind=engine)()
    try:
        record = session.query(CompanyInfo).filter_by(code=code).first()
        if record is not None:
            # 如果存在，更新记录
            for key, value in data.__dict__.items():
                setattr(record, key, value)
        else:
            # 如果不存在，插入新记录
            new_record = CompanyInfo(code=code, name=data['name'], ename=data['ename'], market=data['market'],
                                     idea=data['idea'], ldate=data['ldate'], sprice=data['sprice'], principal=data['principal'],
                                     rdate=data['rdate'], rprice=data['rprice'], instype=data['instype'],
                                     organ=data['organ'], secre=data['secre'], phone=data['phone'], sphone=data['sphone'],
                                     fax=data['fax'], sfax=data['sfax'], email=data['email'], semail=data['semail'],
                                     site=data['site'], post=data['post'], infosite=data['infosite'], oname=data['oname'],
                                     addr=data['addr'], oaddr=data['oaddr'], desc=data['desc'], bscope=data['bscope'],
                                     printype=data['printype'], referrer=data['referrer'], putype=data['putype'],
                                     pe=data['pe'], firgu = data['firgu'],lastgu=data['lastgu'], realgu=data['realgu'],
                                     planm=data['planm'], realm=data['realm'], pubfee=data['pubfee'], collect=data['collect'],
                                     signfee=data['signfee'], pdate=data['pdate'],
                                     )
            session.add(new_record)
        session.commit()
    except Exception as e:
        print(f"Error: {e}")
        session.rollback()
    finally:
        session.close()
